package com.acoreful.acf2admin.web.controller;

import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.acoreful.acf2admin.web.annotation.Token;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/test")
public class TestController {
	private Logger logger=LoggerFactory.getLogger(TestController.class);
	@RequestMapping("index")
	public String main() {
		logger.info("main*****************************************");
		return "main";
	}
	
	@RequestMapping("main2")
	@Token(save=true)
	public String main2() {
		logger.info("main*****************************************");
		return "main";
	}
	
	@RequestMapping("testJson")
	@ResponseBody
	public Map<String, Object> testMapJson(HttpServletResponse resp) {
		Map<String, Object> map=Maps.newHashMap();
		Random a=new Random();
		map.put("success", a.nextBoolean());
		map.put("msg", "sfgsgfsdgfsdgsdgsdg");
		logger.info("Map *****************************************");
		resp.setHeader("aad", UUID.randomUUID().toString());
		return map;
	}
	@RequestMapping("testRedirect")
	public String testRedirect(RedirectAttributes rAttr){
		rAttr.addAttribute("test", "234234234");
		return "redirect:/test";
	}
	@RequestMapping("testRedirect1")
	public String testRedirect1(RedirectAttributes rAttr){
		rAttr.addFlashAttribute("test", "324234234");
		return "redirect:/test";
	}
	@RequestMapping("testRedirect2")
	public String testRedirect2(Model model){
		model.addAttribute("test", "324234234");
		return "redirect:/test";
	}
	@RequestMapping("test")
	public String test(String test,HttpServletRequest request){
		System.out.println(test);
		 Map<String,?> map = RequestContextUtils.getInputFlashMap(request);
		 if(map!=null){
			 System.err.println(map.get("test"));
		 }
		return "main";
	}
	/**
	 * 	HTML 片段测试
	 * @return
	 */
	@RequestMapping("testHtml")
	public String testHtml(){
		logger.info("testHtml*****************************************");
		return "test/htmlFragment";
	}
}
